package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.service.ClazzService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private ScheduleMapper scheduleMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private PurchaseCourseRecordsMapper purchaseCourseRecordsMapper;

    @Override
    public Message deleteByPrimaryKey(Integer classId) {
        return clazzMapper.deleteByPrimaryKey(classId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Clazz record) {
        return clazzMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Clazz record) {
        clazzMapper.insertSelective(record);
        if (record.getStudentId() != null) {
            String[] ids = record.getStudentId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    purchaseCourseRecordsMapper.updatePurchaseClazzIdByStudentIdAndLessonId(record.getClassId(), Integer.parseInt(ID), record.getClassCourseId());
                }
            }
        }
        return Message.success(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer classId) {
        HashMap map = new HashMap();
        Clazz clazz = clazzMapper.selectByPrimaryKey(classId);
        map.put("clazz", clazz);
        map.put("classResponsibleTeacherName", staffMapper.selectByPrimaryKey(clazz.getClassResponsibleTeacherId()).getStaffName());
        map.put("classTeacherName", staffMapper.selectByPrimaryKey(clazz.getClassTeacherId()).getStaffName());
        map.put("classMedicalDirectorName", staffMapper.selectByPrimaryKey(clazz.getClassMedicalDirectorId()).getStaffName());
        map.put("classTeachingDirectorName", staffMapper.selectByPrimaryKey(clazz.getClassTeachingDirectorId()).getStaffName());
        return Message.success(null).add(map);
    }

    @Override
    public Message updateByPrimaryKeySelective(Clazz record) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (record.getStartTimeToString() != null) {
            record.setStartTime(simpleDateFormat.parse(record.getStartTimeToString()));
        }
        if (record.getEndTimeToString() != null) {
            record.setEndTime(simpleDateFormat.parse(record.getEndTimeToString()));
        }
        if (record.getStudentId() != null) {
            String[] ids = record.getStudentId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    purchaseCourseRecordsMapper.updatePurchaseClazzIdByStudentIdAndLessonId(record.getClassId(), Integer.parseInt(ID), record.getClassCourseId());
                }
            }
        }

        return clazzMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Clazz record) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (record.getStartTimeToString() != null) {
            record.setStartTime(simpleDateFormat.parse(record.getStartTimeToString()));
        }
        if (record.getEndTimeToString() != null) {
            record.setEndTime(simpleDateFormat.parse(record.getEndTimeToString()));
        }

        return clazzMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        List<Clazz> clazzList = clazzMapper.selectAll();
        for (Clazz clazz : clazzList) {
            List<Schedule> scheduleList = scheduleMapper.selectByClassIdAndTimeDayZuiJInZhiHou(clazz.getClassId(), new Date());
            if (scheduleList.size() > 0) {
                clazz.setCLASSRoomName(scheduleList.get(0).getRoom().getRoomLoc());
            } else {
                List<Schedule> scheduleList1 = scheduleMapper.selectByClassIdAndTimeDayZuiJInZhiQian(clazz.getClassId(), new Date());
                if (scheduleList1.size() > 0)
                    clazz.setCLASSRoomName(scheduleList1.get(0).getRoom().getRoomLoc());
                else
                    clazz.setCLASSRoomName(null);
            }


        }
        return Message.success(null).add(clazzList);
    }


    @Override
    public Message selectStudentAll() {
        List<Map> mapList = new LinkedList<>();
        List<Clazz> clazzList = clazzMapper.selectAll();
        for (Clazz clazz : clazzList) {
            HashMap map = new HashMap();
            map.put("clazz", clazz);
            List<Student> studentList = new LinkedList<>();
            String[] ids = clazz.getStudentId().split(";");
            if (ids.length > 0) {
                for (String ID : ids) {
                    Student student = studentMapper.selectByPrimaryKey(Integer.parseInt(ID));
                    studentList.add(student);
                }
            }
            map.put("studentList", studentList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectAllByTeachId(int teacherId) {
        return Message.success(null).add(clazzMapper.selectByClassTeacherId(teacherId));
    }

    @Override
    public Message selectStudentInfoByClazzId(int clazzId) {
        Clazz clazz = clazzMapper.selectByPrimaryKey(clazzId);
        List<Student> studentList = new LinkedList<>();
        String[] ids = clazz.getStudentId().split(";");
        if (ids.length > 0) {
            for (String ID : ids) {
                Student student = studentMapper.selectByPrimaryKey(Integer.parseInt(ID));
                studentList.add(student);
            }
        }
        return Message.success(null).add(studentList);
    }

    @Override
    public int batchInsert(List<Clazz> list) {
        return clazzMapper.batchInsert(list);
    }

    @Override
    public Message selectByIdAndTime(int classId, int timeSkcd, int timeSkjc) {


        HashMap map = new HashMap();
        Clazz clazz = clazzMapper.selectByPrimaryKey(classId);
        map.put("clazz", clazz);
        map.put("classResponsibleTeacherName", staffMapper.selectByPrimaryKey(clazz.getClassResponsibleTeacherId()).getStaffName());
        map.put("classTeacherName", staffMapper.selectByPrimaryKey(clazz.getClassTeacherId()).getStaffName());
        map.put("classMedicalDirectorName", staffMapper.selectByPrimaryKey(clazz.getClassMedicalDirectorId()).getStaffName());
        map.put("classTeachingDirectorName", staffMapper.selectByPrimaryKey(clazz.getClassTeachingDirectorId()).getStaffName());
        Schedule schedule = scheduleMapper.selectByClassIdAndTimeSkcdAndTimeSkjc(classId, timeSkcd, timeSkjc);
        Room room = roomMapper.selectByPrimaryKey(schedule.getRoomId());
        map.put("roomName", room.getRoomLoc());
        return Message.success(null).add(map);
    }

    @Override
    public Message selectStudentById(Integer classId) {
        Clazz clazz = clazzMapper.selectByPrimaryKey(classId);
        HashMap map = new HashMap();
        map.put("clazz", clazz);
        List<Student> studentList = new LinkedList<>();
        String[] ids = clazz.getStudentId().split(";");
        if (ids.length > 0) {
            for (String ID : ids) {
                Student student = studentMapper.selectByPrimaryKey(Integer.parseInt(ID));
                studentList.add(student);
            }
        }
        map.put("studentList", studentList);
        return Message.success(null).add(map);
    }



    @Override
    public int updateBatch(List<Clazz> list) {
        return clazzMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Clazz> list) {
        return clazzMapper.updateBatchSelective(list);
    }

    @Override
    public Message judgementClassName(String className) {
        if(clazzMapper.selectByClassName(className) != null){
            return Message.success("该班级名称已存在！");
        }
        return Message.success("该班级名称可用！");
    }

    @Override
    public Message judgementUpdateClassName(int classId, String className) {
        List<Clazz> clazzList = clazzMapper.selectAll();
        for (Clazz clazz : clazzList) {
            if (clazz.getClassName().equals(className)) {
                if (clazz.getClassId() == classId) {
                    return Message.success("该班级名称可用！");
                } else {
                    return Message.success("该班级名称已存在！");
                }
            }
        }
        return Message.success("该班级名称可用！");
    }
}


